<?php
/**
 * Created by PhpStorm.
 * User: songlei
 * Date: 2015/3/6
 * Time: 9:32
 */

/*自定义函数*/

//GUID
function getGuid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));

    $hyphen = chr(45);// "-"
    $uuid = substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12);
    return $uuid;
}

//得到加密后的密码
function getEncPassword($password)
{
    return  md5(C('AUTH_CODE') . md5($password));
}

// 判定是否
function getYesOrNo($yesOrNo)
{
    if((int)$yesOrNo==1) {
        $str = "<span class=\"YesOrNo\">是</span>";
    }else{
        $str = "";
    }

    return $str;
}

/**
 * 格式化datetime
 * @return string
 */
function datetimeShow($date) {
    return date('Y年m月d日 H:i', strtotime(str_replace('/', '-', $date)));
}

/**
 * 生成datetime
 * @return string
 */
function datetime() {
    return date('Y-m-d H:i:s');
}


/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://phparch.cn (Professional PHP Architecture)
 */
function time2Units ($time)
{
    $year   = floor($time / 60 / 60 / 24 / 365);
    $time  -= $year * 60 * 60 * 24 * 365;
    $month  = floor($time / 60 / 60 / 24 / 30);
    $time  -= $month * 60 * 60 * 24 * 30;
    $week   = floor($time / 60 / 60 / 24 / 7);
    $time  -= $week * 60 * 60 * 24 * 7;
    $day    = floor($time / 60 / 60 / 24);
    $time  -= $day * 60 * 60 * 24;
    $hour   = floor($time / 60 / 60);
    $time  -= $hour * 60 * 60;
    $minute = floor($time / 60);
    $time  -= $minute * 60;
    $second = $time;
    $elapse = '';

    $unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',
        '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
    );

    foreach ( $unitArr as $cn => $u )
    {
        if ( $$u > 0 )
        {
            $elapse = $$u . $cn;
            break;
        }
    }

    return $elapse;
}